package mes.kanban.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import mes.kanban.entity.MesJob;
import mes.kanban.respository.UserJobRespository;
import mes.kanban.service.MesJobServiceI;
import mes.kanban.util.PageBean;
import mes.kanban.vo.MesJobVO;

/**
 * 角色作业实现类
 * @author zongrui.liu
 *
 */
@Service
public class MesJobServiceImpl implements MesJobServiceI {
	
	
	@Autowired
	private UserJobRespository mesJobRespository;

	@Override
	public PageBean getJobsForBean(Integer pageNum, Integer pageSize) {
		PageBean pb = new PageBean();
		// 排序规则为降序Direction.ASC   排序字段为 MesJob中的jobContent
		Pageable pa = new PageRequest(pageNum-1, pageSize,Direction.ASC,"jobContent");
		Page<MesJob> jobs = this.mesJobRespository.findAll(pa);
		
		pb.setCurrentPage(pageNum);
		pb.setPageSize(pageSize);
		pb.setList(jobs.getContent());
		pb.setAllCounts((int) jobs.getTotalElements());
		pb.setTotalPage(jobs.getTotalPages());
		
		return pb;
	}

	@Override
	public void save(MesJobVO vo) {
		MesJob job  = new MesJob();
		BeanUtils.copyProperties(vo, job);
		this.mesJobRespository.save(job);
		
	}

	@Override
	public void edit(MesJobVO vo) {
		MesJob job  = new MesJob();
		BeanUtils.copyProperties(vo, job);
		this.mesJobRespository.save(job);
	}

	@Override
	public void deleteJobById(String id) {
		this.mesJobRespository.delete(id);
	}

}
